package com.action;

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.triggers.SimpleTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Demo {

	@Autowired
	public SchedulerFactoryBean baseQuartzScheduler;
//	@Autowired
//	private JobDetailFactoryBean jobDetail;
//	@Autowired
//	private JobDetailFactoryBean jobDetail2;
	
	@RequestMapping("/test")
	public ModelAndView test() throws SchedulerException{
		System.out.println("启动任务！！");
		
		Scheduler scheduler = baseQuartzScheduler.getScheduler();
		
		JobKey jobKey1 = new JobKey("JobDetail1","Job");
		JobKey jobKey2 = new JobKey("JobDetail2","Job");
		
		JobDetail jobDetail1 = scheduler.getJobDetail(jobKey1);
		JobDetail jobDetail2 = scheduler.getJobDetail(jobKey1);
		if(jobDetail1!= null){
			scheduler.deleteJob(jobKey1);
		}
		if(jobDetail2!= null){
			scheduler.deleteJob(jobKey2);
		}
		long l = 6000L;
		Trigger trigger1 = new SimpleTriggerImpl("myTrigger1","asd",
				new Date(),
				null,
                SimpleTrigger.REPEAT_INDEFINITELY,
                l);
		Trigger trigger2 = new SimpleTriggerImpl("myTrigger2","asd",
				new Date(),
				null,
				SimpleTrigger.REPEAT_INDEFINITELY,
				l);
//		jobDetail1 = jobDetail.getObject();
		jobDetail1 = new JobDetailImpl("JobDetail1","Job",com.cluster.quartz.JobDetail.class);
		jobDetail2 = new JobDetailImpl("JobDetail2","Job",com.cluster.quartz.JobDetail2.class);
		scheduler.scheduleJob(jobDetail1, trigger1);
//		scheduler.scheduleJob(jobDetail2, trigger2);
		scheduler.start();
		return new ModelAndView("/jsp/demo.jsp");
	}
}
